home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / UTIL / CD Playthrough 1.5.sit / CD Playthrough 1.5 / Source / Sample Code by Gary Anwyl / setSoundSRC.c next >
C/C++ Source or Header  |  1994-09-26  |  3KB  |  136 lines

  1. /*
  2.     File:            setSoundSRC.c
  3.  
  4.     Contains:        Think C program to set the sound output sample rate
  5.  
  6.     Written by:        Gary Anwyl
  7.  
  8.     Description:    This program finds the "Built-in" sound driver,
  9.                     gets the list of sample rates it supports and 
  10.                     if it supports 44.1 Khz sets it to that rate.
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <console.h>
  17. #include <Components.h>
  18. #include <Sound.h>
  19. #include <SoundInput.h>
  20.  
  21. //    It might be necessary to comment out these definitions depending upon
  22. //    which set of include files are being used.
  23.  
  24. typedef unsigned long UnsignedFixed;        /*unsigned fixed-point number*/
  25. #undef ComponentCallNow
  26. #define ComponentCallNow(callNumber, paramSize)
  27.         {0x2f3c, paramSize, callNumber, 0x7000, 0xA82A}
  28.  
  29. #include "SoundComponents.h"
  30.  
  31.  
  32. #define    kRate22050            (22050L<<16)
  33. #define    kRate24000            (24000L<<16)
  34. #define    kRate44100            (44100L<<16)
  35.  
  36. typedef struct {
  37.     short count;
  38.     UnsignedFixed **rates;
  39.     } SRCInfoStruct;
  40.  
  41.  
  42. void main(int argc, char *argv[])
  43. {
  44.     int                     i;
  45.     OSErr                    err;
  46.     Component                comp;
  47.     ComponentInstance        compInstance;
  48.     ComponentDescription    cd, cd1;
  49.     Handle                    nameHandle;
  50.     SRCInfoStruct            srcInfo;
  51.  
  52.     InitGraf(&qd.thePort);
  53.     
  54.     // Allocate a handle for the name
  55.     nameHandle = NewHandle(256);
  56.     if (nameHandle == 0) {
  57.         DebugStr("¥pNewHandle failed");
  58.         exit(1);
  59.         }
  60.     HLock(nameHandle);
  61.  
  62.     // Search all 'sdev' components for the "Built-In" sound driver
  63.     cd.componentType = 'sdev';
  64.     cd.componentSubType = 0;
  65.     cd.componentManufacturer = 0;
  66.     cd.componentFlagsMask = 0;
  67.     comp = 0;
  68.     
  69.     while (1) {
  70.         comp = FindNextComponent(comp, &cd);
  71.         if (comp == 0) {
  72.             DebugStr("¥pBuilt-In sdev not found");
  73.             goto errExit1;
  74.             }
  75.         err = GetComponentInfo(comp, &cd1, nameHandle, 0 , 0);
  76.         if (err != noErr) {
  77.             DebugStr("¥pGetComponentInfo failed");
  78.             goto errExit1;
  79.             }
  80.         
  81.         // nameHandle is a pascal string. See if it is "¥pBuilt-in"
  82.         if (strncmp("Built-in", (*nameHandle)+1, (**nameHandle)) == 0)
  83.             break;
  84.         }
  85.         
  86.     // Open the sound component
  87.     compInstance = OpenComponent(comp);
  88.     if (compInstance == 0) {
  89.         DebugStr("¥pOpenComponent failed");
  90.         goto errExit1;
  91.         }
  92.     
  93.     // See if 44.1KHz is supported. This returns a range or an array of rates
  94.     err = SoundComponentGetInfo(compInstance, 0, siSampleRateAvailable, &srcInfo);
  95.     if (err != noErr) {
  96.         DebugStr("¥pSoundComponentGetInfo failed");
  97.         goto errExit2;
  98.         }
  99.     
  100.     if (srcInfo.count == 0) {
  101.         // The lower and upper bounds of a range was returned.
  102.         if ((*srcInfo.rates)[0] > kRate44100 || (*srcInfo.rates)[1] < kRate44100) {
  103.             DebugStr("¥p44100 not supported");
  104.             goto errExit2;
  105.             }
  106.         }
  107.     else {
  108.         // Search array of available rates for 44.1 KHz
  109.         for (i=0; 1; i++) {
  110.             if (i==srcInfo.count) {
  111.                 DebugStr("¥p44100 not supported");
  112.                 goto errExit2;
  113.                 }
  114.             if ((*srcInfo.rates)[i] == kRate44100)
  115.                 break;
  116.             }
  117.         }
  118.         
  119.     // Set the sample rate
  120.     err = SoundComponentSetInfo(compInstance, 0, siSampleRate, (void *)kRate44100);
  121.     if (err != noErr) {
  122.         DebugStr("¥pSoundComponentSetInfo failed");
  123.         goto errExit2;
  124.         }
  125.     
  126.     CloseComponent(compInstance);
  127.     exit(0);
  128.     
  129.     // Error exit points
  130. errExit2:        
  131.     CloseComponent(compInstance);
  132. errExit1:
  133.     exit(1);
  134. }
  135.         
  136.